package com.situ.ssm.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.ssm.entity.Banji;
import com.situ.ssm.entity.PageInfo;
import com.situ.ssm.entity.Result;
import com.situ.ssm.entity.Student;
import com.situ.ssm.service.IBanjiService;
import com.situ.ssm.service.IStudentService;

@Controller
@RequestMapping(value = "/student")
public class StudentController {
	
//	@InitBinder
//	public void initBinder(WebDataBinder binder) {
//	    // 把字符创的日期类型转换成Date类型
//	    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//	    simpleDateFormat.setLenient(false);
//	    binder.registerCustomEditor(Date.class, new CustomDateEditor(simpleDateFormat, true));
//	}
	
	@Autowired
	private IStudentService studentService;
	@Autowired
	private IBanjiService banjiService;
	
	@RequestMapping(value = "/getStudentListPage")
	public String getStudentListPage() {
		
		return "/student_list.jsp";
	}
	
	@RequestMapping(value = "/findByPage")
	@ResponseBody
	public Result findByPage(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "5")Integer limit) {
		System.out.println("StudentController.findByPage()");
		// 原来展示页面需要的是PageInfo这个对象，现在需要的是Result
		Result result = studentService.findByPage(page, limit);
		System.out.println(result);
		return result;
	}

	@RequestMapping(value = "/findByPage2")
	public String findByPage2(@RequestParam(defaultValue = "1")Integer pageNo, @RequestParam(defaultValue = "5")Integer pageSize,Model model) {
		System.out.println("StudentController.findByPage()");
		PageInfo pageInfo = studentService.findByPage2(pageNo, pageSize);
		System.out.println(pageInfo);
		
		model.addAttribute("pageInfo", pageInfo);
		return "/student_list_back.jsp";
	}
	
	@RequestMapping(value = "/getStudentInsertPage")
	public String getStudentInsertPage(Model model) {
		System.out.println("StudentController.getStudentInsertPage()");
		List<Banji> list = banjiService.findAll();
		model.addAttribute("list", list);
		return "/student_insert.jsp";
	}
	
	@RequestMapping(value = "/insert")
	@ResponseBody
	public Result insert(Student student) {
		System.out.println("StudentController.insert()");
		System.out.println(student);
		boolean isSuccess = studentService.insert(student);
		
		if (isSuccess) {
			return Result.success("插入成功");
		} else {
			return Result.error("插入失败");
		}
	}
	
	@RequestMapping(value = "/deleteById")
	@ResponseBody
	public Result deleteById(Integer id) {
		System.out.println("StudentController.deleteById()");
		System.out.println("id: " + id);
		boolean isSuccess = studentService.deleteById(id);
		
		if (isSuccess) {
			return Result.success("删除成功");
		} else {
			return Result.error("删除失败");
		}
	}
	
	
}